
var app = getApp();
const util = require('../../utils/util.js')
const config = require('../../utils/config.js');
var my = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {

    //接口地址
    groupList: "groupon/list",
    addToUser:"groupon/addToUser",
    addGRecord:"groupon/addGRecord",
    gList:[],
    coupon_ZN:"",
    cid:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 1500
    }) 
    var that = this
    that.groupList()
  },


  groupList:function(){

    var that = this;
    my.request({
      url: that.data.groupList,
      //data: params,
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
      console.log(res.data.data)

      let curList = res.data.data;
       curList.forEach(item=>{
        //转换中文类型
        let configs = {
          general_coupon:"优惠券",
          cash:"代金券",
          discount:"折扣券",
          gift:"兑换券",
          groupon:"团购券"
        }
        //  if (item.card.cart_type =="general_coupon"){
        //   // general_coupon();
        //  }
       // console.log(item.card.cart_type);
        item.card.cart_typezh = configs[item.card.card_type];
         if (item.card[item.card.card_type].base_info.date_info.hasOwnProperty("end_timestamp")){
           let cur = item.card[item.card.card_type].base_info.date_info.end_timestamp
           item.card[item.card.card_type].base_info.date_info.end_timestamp = util.formatTime(new Date(cur * 1000)) +"过期"
           item.hasTime = true;
         }else{
           //领取后当天生效30天有效
           //fixed_term
           let curfixed_term = item.card[item.card.card_type].base_info.date_info.fixed_term
           item.card[item.card.card_type].base_info.date_info.end_timestamp = `领取后当天生效${curfixed_term}天有效`;
           item.hasTime = false;
         }
       }),
        that.setData({
           "gList": res.data.data
        })
        
       // console.log(that.data.gList)
      }

    })
  },
  addcard:function(e){
    //console.log(e.currentTarget.dataset.cid)
    var that=this
    let cid = e.currentTarget.dataset.cid
    that.setData({
      "cid": cid
    })

    my.request({
      url: that.data.addToUser,
      data: {
        id: cid
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data.data.cardExt)
        wx.addCard({
          cardList: [
            {
              cardId: res.data.data.cardId,
              cardExt: JSON.stringify(res.data.data.cardExt)
            }
          ],
          success: function (res) {
            console.log(res.cardList) // 卡券添加结果
            wx.showToast({
              title: '成功领取到卡包',
              icon: 'success',
              duration: 2000
            })

            that.addGRecord(); 

          }
        })
      }
    })

  },

  addGRecord: function () {
    //console.log(e.currentTarget.dataset.cid)
    var that = this


    my.request({
      url: that.data.addGRecord,
      data: {
        cId: that.data.cid
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
         console.log("添加到我的优惠卷")
      }
    })

  },

 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function (options) {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (options) {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function (options) {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (options) {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {

  }
})



